import QtQuick
import QtQuick.Controls
import QtQuick.Controls.impl
import QtQuick.Templates as T

T.ComboBox {
    id: control

    property color backColor: "#44484d" // 背景颜色
    property color textColor: "#e1e4e8" // 字体颜色
    property color hoverColor: "#616263" // 悬停背景颜色
    property double itemRadius: 8   // 圆角
    property real textSize: 14 // 字体大小
    property int animationTime: 250 // 颜色过渡动画时间
    property real borderWidth: 0 // 边框宽度
    property color borderColor: "#888888" // 边框颜色
    property bool backDisplay: false // box列表背景色显示与否 默认false透明

    implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
                            implicitContentWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             implicitContentHeight + topPadding + bottomPadding,
                             implicitIndicatorHeight + topPadding + bottomPadding)

    leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
    rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)

    // box列表各Item
    delegate: ItemDelegate {
        width: control.width
        height: control.height
        highlighted: control.highlightedIndex === index
        hoverEnabled: control.hoverEnabled
        // 内容
        contentItem: Text {
            text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
            color: textColor
            font.pixelSize: textSize
            verticalAlignment: Text.AlignVCenter
        }
        // 背景
        background: Rectangle {
            radius: itemRadius
            color: hovered ? hoverColor : backColor
            border.width: borderWidth
            border.color: borderColor

            Behavior on color {

                ColorAnimation {
                    duration: animationTime
                }
            }
        }
    }
    // box图标
    indicator: ColorImage {
        x: 0 ? control.padding : control.width - width - control.padding
        y: control.topPadding + (control.availableHeight - height) / 2
        width: 30
        color: control.palette.dark
        source: "qrc:/qt-project.org/imports/QtQuick/Controls/Basic/images/double-arrow.png"
        opacity: enabled ? 1 : 0.3
    }
    // box内容
    contentItem: T.TextField {
        leftPadding: !control.mirrored ? 8 : control.editable && activeFocus ? 3 : 1
        rightPadding: control.mirrored ? 8 : control.editable && activeFocus ? 3 : 1
        topPadding: 4 - control.padding
        bottomPadding: 4 - control.padding

        text: control.editable ? control.editText : control.displayText
        font.pixelSize: textSize

        enabled: control.editable
        autoScroll: control.editable
        readOnly: control.down
        inputMethodHints: control.inputMethodHints
        validator: control.validator
        selectByMouse: control.selectTextByMouse

        color: textColor
        verticalAlignment: Text.AlignVCenter
    }
    // box背景
    background: Rectangle {
        implicitWidth: 140
        implicitHeight: 40
        radius: itemRadius

        color: control.down ? hoverColor : backColor
        border.width: borderWidth
        border.color: borderColor
        visible: !control.flat || control.down
    }
    // 弹出框
    popup: T.Popup {
        y: control.height
        width: control.width
        height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
        topMargin: 6
        bottomMargin: 6

        // 弹出框内容
        contentItem: ListView {
            clip: true
            implicitHeight: contentHeight
            model: control.delegateModel
            currentIndex: control.highlightedIndex
            highlightMoveDuration: 0

            delegate: Rectangle {
                z: 10
                width: parent.width
                height: parent.height
                color: "transparent"
                radius: itemRadius
            }

            T.ScrollIndicator.vertical: ScrollIndicator { }
        }

        // 弹出框背景
        background: Rectangle {
            color: backDisplay ? backColor : "transparent"
            radius: itemRadius
            border.width: borderWidth
            border.color: borderColor
        }
    }
}
